package com.shawn.rabbitmq.publishsubsribemode;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;
import java.util.concurrent.TimeoutException;

/**
 * @ClassName Producer
 * @Description TODO
 * @Author shawn
 * @date 2021/9/22 9:39 下午
 * @Version 1.0
 */
public class EmitLog {
    public final static String EXCHANGE_NAME = "logs";

    public static void main(String[] args) throws IOException, TimeoutException {
        //创建一个连接工厂
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("192.168.64.12");
        factory.setUsername("shawn");
        factory.setPassword("shawn");
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();
        //声明一个fanout类型的交换机
        channel.exchangeDeclare(EXCHANGE_NAME, "fanout");
        System.out.println("请输入内容：");
        Scanner scanner = new Scanner(System.in);
        while(scanner.hasNext()){
            String message = scanner.next();
            channel.basicPublish(EXCHANGE_NAME, "", null, message.getBytes());
        }
    }
}
